package com.qyhl.webtv.module_live.teletext.nanbu.fragment;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.makeramen.roundedimageview.RoundedImageView;
import com.qyhl.webtv.basiclib.base.BaseFragment;
import com.qyhl.webtv.basiclib.utils.NetUtil;
import com.qyhl.webtv.basiclib.utils.StringUtils;
import com.qyhl.webtv.commonlib.constant.ARouterPathConstant;
import com.qyhl.webtv.commonlib.entity.live.NanBuLiveTagBean;
import com.qyhl.webtv.commonlib.entity.live.TeleTextBean;
import com.qyhl.webtv.commonlib.utils.router.RouterManager;
import com.qyhl.webtv.module_live.R;
import com.qyhl.webtv.module_live.R2;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.weavey.loading.lib.LoadingLayout;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;
import com.zhy.view.flowlayout.FlowLayout;
import com.zhy.view.flowlayout.TagAdapter;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


public class NanBuLiveFragment extends BaseFragment implements NanBuLiveContract.NanBuLiveView {
    @BindView(R2.id.recycle_view)
    RecyclerView recycleView;
    @BindView(R2.id.refresh)
    SmartRefreshLayout refresh;
    @BindView(R2.id.load_mask)
    LoadingLayout loadMask;

    private String tagId;

    private String sceneId = "0";

    private NanBuLivePresenter mPresenter;

    private List<TeleTextBean> mDataList = new ArrayList<>();
    private CommonAdapter<TeleTextBean> commonAdapter;

    public void setTagId(String tagId) {
        this.tagId = tagId;
    }

    public static NanBuLiveFragment newInstance(String tagId) {
        NanBuLiveFragment fragment = new NanBuLiveFragment();
        fragment.setTagId(tagId);
        return fragment;
    }

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.live_fragment_nanbu_live_child, null);
    }

    @Override
    protected void create() {
        loadMask.setStatus(LoadingLayout.Loading);

        mPresenter = new NanBuLivePresenter(this);

        refresh.setRefreshHeader(new MaterialHeader(getContext()));
        refresh.setRefreshFooter(new ClassicsFooter(getContext()));
        refresh.setEnableLoadMore(true);

        recycleView.setLayoutManager(new LinearLayoutManager(getContext()));
        recycleView.setAdapter(commonAdapter = new CommonAdapter<TeleTextBean>(getContext(), R.layout.live_item_nanbu_live, mDataList) {
            @Override
            protected void convert(ViewHolder holder, TeleTextBean teleTextBean, int position) {
                if (teleTextBean.getTagList() != null && teleTextBean.getTagList().size() > 0) {
                    holder.setVisible(R.id.tag_layout, true);

                    TagFlowLayout tagFlowLayout = holder.getView(R.id.tag_layout);
                    tagFlowLayout.setAdapter(new TagAdapter<NanBuLiveTagBean>(teleTextBean.getTagList()) {
                        @SuppressLint("SetTextI18n")
                        @Override
                        public View getView(FlowLayout parent, int position, NanBuLiveTagBean bean) {
                            TextView tv = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.live_item_nanbu_tag,
                                    tagFlowLayout, false);
                            tv.setText(bean.getTypeName());
                            return tv;
                        }
                    });


                } else {
                    holder.setVisible(R.id.tag_layout, false);
                }

                if (StringUtils.isNotEmpty(teleTextBean.getHits())) {
                    holder.setText(R.id.scan_num, StringUtils.transformNum(teleTextBean.getHits()));
                } else {
                    holder.setText(R.id.scan_num, "0");
                }

                RoundedImageView cover = holder.getView(R.id.cover);
                Glide.with(NanBuLiveFragment.this)
                        .load(teleTextBean.getCoverPic())
                        .apply(new RequestOptions().placeholder(R.drawable.cover_large_default).error(R.drawable.cover_large_default))
                        .into(cover);

                holder.setText(R.id.title, teleTextBean.getTitle());
                holder.setVisible(R.id.status, true);
                switch (teleTextBean.getStatus()) {
                    case "6":
                        holder.setImageResource(R.id.status, R.drawable.live_nanbu_tag_forcast);
                        break;
                    case "7":
                        holder.setImageResource(R.id.status, R.drawable.live_nanbu_tag_review);
                        break;
                    case "2":
                        holder.setImageResource(R.id.status, R.drawable.live_nanbu_tag_live);
                        break;
                    case "3":
                        holder.setImageResource(R.id.status, R.drawable.live_nanbu_tag_end);
                        break;
                    default:
                        holder.setVisible(R.id.status, false);
                        break;
                }
            }
        });

        mPresenter.getList(tagId, sceneId);

    }

    @Override
    protected void setListener() {
        loadMask.setOnReloadListener(new LoadingLayout.OnReloadListener() {
            @Override
            public void onReload(View v) {
                loadMask.setReloadButtonText("加载中...");
                sceneId = "0";
                mPresenter.getList(tagId, sceneId);
            }
        });

        refresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                sceneId = "0";
                mPresenter.getList(tagId, sceneId);
            }
        });

        refresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                mPresenter.getList(tagId, sceneId);
            }
        });

        commonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder viewHolder, int i) {
                Bundle bundle = new Bundle();
                bundle.putString("id", mDataList.get(i).getId() + "");
                RouterManager.router(ARouterPathConstant.NANBU_LIVE_DETAIL, bundle);
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder viewHolder, int i) {
                return false;
            }
        });
    }

    @Override
    public void setData(List<TeleTextBean> beanList, boolean isMore) {
        loadMask.setStatus(LoadingLayout.Success);
        loadMask.setReloadButtonText("点击重试~");
        if (isMore) {
            refresh.finishLoadMore();
        } else {
            refresh.finishRefresh();
            mDataList.clear();
        }
        sceneId = beanList.get(beanList.size() - 1).getId() + "";
        mDataList.addAll(beanList);
        commonAdapter.notifyDataSetChanged();
    }

    @Override
    public void setError(String msg, boolean isMore) {
        if (isMore) {
            refresh.finishLoadMore();
            if (!NetUtil.isNetworkAvalible(getContext())) {
                showToast("网络异常，请检查您的网络！");
            } else if (msg.contains("暂无")) {
                showToast("暂无更多直播！");
            } else {
                showToast(msg);
            }
        } else {
            refresh.finishRefresh();
            loadMask.setStatus(LoadingLayout.Error);
            loadMask.setReloadButtonText("点击重试~");
            if (!NetUtil.isNetworkAvalible(getContext())) {
                loadMask.setErrorImage(R.drawable.error_network);
                loadMask.setErrorText("网络异常，请检查您的网络！");
            } else if (msg.contains("暂无")) {
                loadMask.setErrorImage(R.drawable.empty_content);
                loadMask.setErrorText(msg);
            } else {
                loadMask.setErrorImage(R.drawable.error_content);
                loadMask.setErrorText(msg);
            }
        }
    }

    @Override
    protected void onFirstVisible() {

    }

    @Override
    protected void onVisible() {

    }

    @Override
    protected void onHide() {

    }


}
